#include <iostream>
#include <unistd.h>
#include <ctime>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};

enum
{
    Screen = 10,
    OneFile,
    ClassFile
};

const int default_style = Screen;
const std::string default_filename = "log";
const std::string logdir = "log";
std::string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknow";
    }
}

std::string TimeStampExLocalTime()
{
    time_t curtime = time(nullptr);
    struct tm *curr = localtime(&curtime);
    char timebuffer[128];
    snprintf(timebuffer, sizeof(timebuffer), "%d-%d-%d %d:%d:%d",
             curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday, curr->tm_hour, curr->tm_min, curr->tm_sec);
    return timebuffer;
}
class log
{
public:
    log()
        : _style(default_style), _filename(default_filename)
    {
        mkdir(logdir.c_str(), 0775);
    }

    void Enable(int style)
    {   
        _style = style;
    }
    void WriteToOneFile(std::string logname, std::string message)
    {
        int fd = open(logname.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
        if(fd < 0)  return;
        
        write(fd, message.c_str(), message.size());
        close(fd);

    }
    void WriteToClassFile(std::string levelstr, std::string message)
    {
        std::string logname = "log/";
        logname += _filename;
        //logname += "." + levelstr;
        std::cout << logname << std::endl;
        WriteToOneFile(logname, message);
    }
    void WriteLog(std::string levelstr, std::string message)
    {
        switch (_style)
        {
        case Screen:
            std::cout << message << std::endl;
            break;

        case OneFile:
            WriteToClassFile("all", message);
            break;
        case ClassFile:
            std::cout << 1 << std::endl;
            WriteToClassFile(levelstr, message);
            break;
        default:
            break;
        }
    }
    void logMessage(int Level, const char *format, ...)
    {
        char leftbuffer[1024];
        std::string levelstr = LevelToString(Level);
        std::string curtime = TimeStampExLocalTime();
        std::string pid = std::to_string(getpid());
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%s][%s]", levelstr.c_str(), curtime.c_str(), pid.c_str());

        char rightbuffer[1024];
        va_list args;
        va_start(args, format);
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, args);
        va_end(args);

        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;
        WriteLog(levelstr, loginfo);
    }

    ~log() {};

private:
    int _style;
    std::string _filename;
};


log lg;

class Conf
{
    public:
        Conf()
        {
            lg.Enable(Screen);
        }
        ~Conf()
        {}
};

Conf conf;